package com.sanben.tag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanben.common.util.BeanCopyUtils;
import com.sanben.tag.data.dto.TagRelationshipDto;
import com.sanben.tag.data.req.TagRelationshipReq;
import com.sanben.tag.data.vo.TagRelationshipVo;
import com.sanben.tag.entity.Tag;
import com.sanben.tag.entity.TagRelationship;
import com.sanben.tag.mapper.TagRelationshipMapper;
import com.sanben.tag.service.ITagRelationshipService;
import com.sanben.tag.service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cyh
 * @description: 标签业务对应关系表 服务实现类
 * @date: 2020-08-18
 */
@Service
public class TagRelationshipServiceImpl extends ServiceImpl<TagRelationshipMapper, TagRelationship> implements ITagRelationshipService {

    @Autowired
    private ITagService tagService;

    @Override
    public IPage<TagRelationshipVo> page(TagRelationshipReq req) {
        Page<TagRelationshipVo> page = new Page<>(req.getPageNum(), req.getPageSize());
        page.setRecords(this.baseMapper.page(page, req));
        return page;
    }

    @Override
    public List<TagRelationship> lists() {
        return this.list();
    }

    @Override
    public Boolean save(TagRelationshipDto dto) {
        return this.save(BeanCopyUtils.copy(dto, TagRelationship.class));
    }

    @Override
    public Boolean updateById(TagRelationshipDto dto) {
        return this.updateById(BeanCopyUtils.copy(dto, TagRelationship.class));
    }


    @Override
    public Boolean deleteLogic(List<Integer> toIntList) {
        return this.removeByIds(toIntList);
    }

    @Override
    public TagRelationship detail(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<String> getTagList(Integer businessId) {
        LambdaQueryWrapper<TagRelationship> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(TagRelationship::getBusinessId, businessId);
        List<Integer> tagIds = this.list(lambdaQuery).stream().map(p->p.getTagId()).collect(Collectors.toList());
        LambdaQueryWrapper<Tag> tagLambdaQuery=new LambdaQueryWrapper<>();
        tagLambdaQuery.in(Tag::getId,tagIds);
        return tagService.list(tagLambdaQuery).stream().map(p->p.getName()).collect(Collectors.toList());
    }
}
